package com.code.springboot3.controller;

import com.code.springboot3.common.BusinessException;
import com.code.springboot3.common.Result;
import com.code.springboot3.model.dto.AddArticleDto;
import com.code.springboot3.model.dto.AddCategoryDto;
import com.code.springboot3.model.dto.UpdateArticleDto;
import com.code.springboot3.model.dto.UpdateCategoryDto;
import com.code.springboot3.model.entity.Article;
import com.code.springboot3.model.vo.CategoryVo;
import com.code.springboot3.service.ArticleService;
import com.code.springboot3.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import com.code.springboot3.validation.ArticleId;

import java.util.List;

/**
 * 文章管理相关接口
 */
@Tag(name = "文章管理相关接口")
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @RequiresAuthentication
    @RequiresRoles("user")
    @Operation(summary = "根据token新增文章")
    @PostMapping
    public Result<String> addArticle(@RequestBody AddArticleDto addArticleDto, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        articleService.addCategory(addArticleDto, token);
        return Result.success(null);
    }

    @RequiresAuthentication
    @Operation(summary = "获取文章详情")
    @GetMapping("/{id}")
    public Result<Article> getArticleDetail(@PathVariable Integer id, HttpServletRequest request) {
        Article article = articleService.getArticleDetail(id, request);
        return Result.success(article);
    }

    @RequiresAuthentication
    @RequiresRoles("user")
    @Operation(summary = "更新文章")
    @PutMapping
    public Result<String> updateArticle(@RequestBody UpdateArticleDto updateArticleDto, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        articleService.updateArticleDetail(updateArticleDto, token);
        return Result.success(null);
    }

    @Operation(
        summary = "删除文章",
        description = "删除指定的文章，需要用户登录且具有user角色。只能删除自己创建的文章。",
        parameters = {
            @Parameter(
                name = "id",
                description = "文章ID",
                required = true,
                schema = @Schema(type = "integer", minimum = "1")
            )
        }
    )
    @RequiresAuthentication
    @RequiresRoles("user")
    @ApiResponse(responseCode = "200", description = "删除成功")
    @ApiResponse(responseCode = "400", description = "参数错误")
    @ApiResponse(responseCode = "403", description = "没有权限")
    @ApiResponse(responseCode = "404", description = "文章不存在")
    @DeleteMapping("/{id}")
    public Result<Void> deleteArticle(
            @PathVariable @ArticleId Integer id,
            HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        articleService.deleteArticle(id, token);
        return Result.success();
    }
}
